<template>
	<view class="upload-video">
		<view class="video-upload-btn video-item flex-all-centen"
		@tap="chooseVideo" v-if="!src">
			<view class="icon-container">
				<view class="iconfont icon-xiangji"></view>
				<view>视频/录像</view>
			</view>
		</view>
		<!-- <template v-else>
			<video :src="src" class="video"></video>
		</template> -->
	</view>
</template>

<script>
	var sourceType = [
		['camera'],
		['album'],
		['camera', 'album']
	]
	export default {
		data() {
			return {
				title: 'chooseVideo',
				sourceTypeIndex: 2,
				sourceType: ['拍摄', '相册', '拍摄或相册'],
				src: '',
				cameraList: [{
						value: 'back',
						name: '后置摄像头',
						checked: 'true'
					},
					{
						value: 'front',
						name: '前置摄像头'
					},
				],
				cameraIndex: 0
			};
		},
		onUnload() {
			this.src = '';
				this.sourceTypeIndex = 2;
				this.sourceType = ['拍摄', '相册', '拍摄或相册'];
		},
		methods: {
			radioChange(evt) {
				for (let i = 0; i < this.cameraList.length; i++) {
					if (this.cameraList[i].value === evt.detail.value) {
						this.cameraIndex = i;
						break;
					}
				}
			},
			sourceTypeChange: function(e) {
				
				this.sourceTypeIndex = parseInt(e.detail.value)
			},
			chooseVideo: function() {
				uni.chooseVideo({
					camera: this.cameraList[this.cameraIndex].value,
					sourceType: sourceType[this.sourceTypeIndex],
					success: (res) => {
						this.src = res.tempFilePath
					},
					fail: (err) => {
						// #ifdef MP
						uni.getSetting({
							success: (res) => {
								let authStatus = false;
								switch (this.sourceTypeIndex) {
									case 0:
										authStatus = res.authSetting['scope.camera'];
										break;
									case 1:
										authStatus = res.authSetting['scope.album'];
										break;
									case 2:
										authStatus = res.authSetting['scope.album'] && res.authSetting['scope.camera'];
										break;
									default:
										break;
								}
								if (!authStatus) {
									uni.showModal({
										title: '授权失败',
										content: 'Hello uni-app需要从您的相机或相册获取视频，请在设置界面打开相关权限',
										success: (res) => {
											if (res.confirm) {
												uni.openSetting()
											}
										}
									})
								}
							}
						})
						// #endif
					}
				})
			}
		}
	}
</script>

<style lang="scss" scoped>
	.upload-video{
		margin: 10rpx;
		flex-wrap: wrap;
		.video-item{
			width: 200rpx;
			height: 200rpx;
			overflow: hidden;
			text-align: center;
			margin: 10rpx;
			background-color: #f2f2f2;
			image{
				max-width: 200rpx;
				max-height: 200rpx;
			}
		}
		.video-upload-btn{
			background-color: #f2f2f2;
			.icon-container{
				text-align: center;
				font-size: 24rpx;
				color: #888888;
			}
		}
	}
</style>
